<template>
  <div class="app-container">
    <!--按钮-->
    <el-row :gutter="20" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="check-circle" plain size="mini" @click="Test2()">路径模式流程处理单(动态)</el-button>
      </el-col>
    </el-row>

    <!--流程处理单对话框   通用-->
    <el-dialog :title="ProcessSubmitFormDialog" :visible.sync="open" width="800px" :close-on-click-modal="false"
      :append-to-body="true" @close="closeDialog">
      <el-form class="ProcessSubmitForm_class" ref="ProcessSubmitForm" :model="ProcessSubmitForm" :rules="rules">
        <el-row>
          <el-col>
            <textarea v-model="ProcessSubmitForm.Remark" placeholder="请输入用户审批或办理意见" rows="7"
              style="resize: none;width: 100%;border: 1px solid gainsboro;"></textarea>
          </el-col>
          <el-col style="margin-left:4px;">
            <el-table class="routerData-el-table" ref="singleTable" :data="routerData" :show-header="false" size="mini"
              highlight-current-row @current-change="handleCurrentChange">
              <el-table-column property="routerName" show-overflow-tooltip></el-table-column>
            </el-table>
          </el-col>
        </el-row>

        <!--动态表单开始-->
        <div v-for="(item,index) in ProcessSubmitForm.dynamicUserTaskList" :key="index">
          <el-row class="el-row" v-if="index%2==0">
            <el-col>
              <fieldset class="fieldset">
                <legend class="legend">item.TaskName</legend>
                <!--供选人群-->
                <div class="div_left">
                  <el-tree :data="item.userDataTree" :props="defaultProps" @check-change="handleCheckChange"
                    show-checkbox></el-tree>
                </div>
                <!--按钮-->
                <div class="div_mid">
                  <i class="el-icon-d-arrow-right"></i>
                </div>
                <!--入选人员-->
                <div class="div_right" id="selectedUser" +index>

                </div>
              </fieldset>
            </el-col>
          </el-row>
          <el-col v-else>
            <fieldset class="fieldset">
              <legend class="legend">item.TaskName</legend>
              <!--供选人群-->
              <div class="div_left">
                <el-tree :data="item.userDataTree" :props="defaultProps" @check-change="handleCheckChange"
                  show-checkbox></el-tree>
              </div>
              <!--按钮-->
              <div class="div_mid">
                <i class="el-icon-d-arrow-right"></i>
              </div>
              <!--入选人员-->
              <div class="div_right" id="selectedUser" +index>

              </div>
            </fieldset>
          </el-col>
        </div>
        <!--动态表单结束-->
        <el-row class="el-row">
          <el-col>
            <fieldset class="fieldset">
              <legend class="legend">室审核</legend>
              <!--供选人群-->
              <div class="div_left">
                <el-tree :data="data" :props="defaultProps" @check-change="handleCheckChange" show-checkbox></el-tree>
              </div>
              <!--按钮-->
              <div class="div_mid">
                <i class="el-icon-d-arrow-right"></i>
              </div>
              <!--入选人员-->
              <div class="div_right" id="selectedUser">

              </div>
            </fieldset>
          </el-col>
          <el-col>
            <fieldset class="fieldset">
              <legend class="legend">部门审核</legend>
              <!--供选人群-->
              <div class="div_left">
                <el-tree :data="data2" :props="defaultProps" @node-click="handleNodeClick2" show-checkbox></el-tree>
              </div>
              <!--按钮-->
              <div class="div_mid">
                <i class="el-icon-d-arrow-right"></i>
              </div>
              <!--入选人员-->
              <div class="div_right"></div>
            </fieldset>
          </el-col>
        </el-row>
        <el-row class="el-row">
          <el-col>
            <fieldset class="fieldset">
              <legend class="legend">综合部审核</legend>
              <!--供选人群-->
              <div class="div_left">
                <el-tree :data="data3" :props="defaultProps" @node-click="handleNodeClick3" show-checkbox></el-tree>
              </div>
              <!--按钮-->
              <div class="div_mid">
                <i class="el-icon-d-arrow-right"></i>
              </div>
              <!--入选人员-->
              <div class="div_right"></div>
            </fieldset>
          </el-col>
          <el-col>
            <fieldset class="fieldset">
              <legend class="legend">综合部审核</legend>
              <!--供选人群-->
              <div class="div_left">

              </div>
              <!--按钮-->
              <div class="div_mid">
                <i class="el-icon-d-arrow-right"></i>
              </div>
              <!--入选人员-->
              <div class="div_right"></div>
            </fieldset>
          </el-col>
        </el-row>


        <!--
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" icon="el-icon-check" @click="submitForm">提交</el-button>
          <el-button type="info" icon="el-icon-message" @click="saveForm">保存并关闭</el-button>
          <el-button type="warning" icon="el-icon-star-off" plain>保存为常用意见</el-button>
          <el-button icon="el-icon-close" @click="cancel">取 消</el-button>
          <el-select v-model="value" placeholder="选择常用意见">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </div>
        -->
        <div class="bottom-button">
          <el-button type="primary" icon="el-icon-check" @click="submitForm">提交</el-button>
          <el-button type="info" icon="el-icon-message" @click="saveForm">保存并关闭</el-button>
          <el-button type="warning" icon="el-icon-star-off" plain>保存为常用意见</el-button>
          <el-button icon="el-icon-close" @click="cancel">取 消</el-button>
          <el-select v-model="value" placeholder="选择常用意见">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </div>

      </el-form>
    </el-dialog>

  </div>
</template>

<script>
  import {
    testFwgl
  } from "@/api/system/oa/test";

  import {
    initCandidate
  } from "@/api/process/processForm.js";

  export default {
    name: "test",
    data() {
      return {
        ProcessSubmitFormDialog: "流程提交",
        // 遮罩层
        loading: true,
        dynamicUserTaskList: [],
        data: [],
        selectedData: [],
        data2: [],
        data3: [],
        defaultProps: {
          children: 'children',
          label: 'label'
        },
        routerData: [{
          id: '11111111111111',
          routerName: '送室审核'
        }, {
          id: '222',
          routerName: '送部门审核-超长数据-送送部门审核送送部门审核送送部门审核送送部门审核送送部门审核'
        }, {
          id: '11111111111111',
          routerName: '送室审核'
        }, {
          id: '222',
          routerName: '送送部门审核'
        }],

        // 选中数组
        options: [{
          value: '0',
          label: '选择常用意见'
        }, {
          value: '选项1',
          label: '请部门领导审核'
        }, {
          value: '选项2',
          label: '请市场部、财务部、综合部、大数据运营中心、采购部审核'
        }, {
          value: '选项3',
          label: '蚵仔煎'
        }, {
          value: '选项4',
          label: '龙须面'
        }, {
          value: '选项5',
          label: '北京烤鸭'
        }],
        value: '0',
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 参数表格数据
        FileWordList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 类型数据字典
        typeOptions: [],
        // 日期范围
        dateRange: [],
        // 状态数据字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          queryInfo: undefined
        },
        // 表单参数
        ProcessSubmitForm: {},
        // 表单校验
        rules: {
          Remark: [{
            required: true,
            message: "意见不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {

    },
    methods: {
      /** 查询参数列表 */
      Test2() {
        this.open = true;
        this.value = "0";
        this.dynamicUserTaskList = [{
          routerName: "送室审核",
          tasks: [{TaskName: "室审核"} ]
        }];
        initCandidate("null").then(response => {
          this.data = response.data;
        });
        initCandidate("103").then(response => {
          this.data2 = response.data;
        });
        initCandidate("204").then(response => {
          this.data3 = response.data;
        });
      },
      handleCurrentChange() {

      },
      //递归找选择组织下的所有用户
      orgNextUser(obj, tdata) {
        console.log(tdata.id + "-->" + tdata.label + "->" + tdata.children);
        // if (tdata.children == undefined)
        //   if (!this.isExistSpan(obj, "<span value=\"" + tdata.id + "\">" + tdata.label + "</span><br>"))
        //     obj.innerHTML += "<span value=\"" + tdata.id + "\">" + tdata.label + "</span><br>"
        // else {
        for (let m = 0; m < tdata.children.length; m++) {
          if (tdata.children[m].children == undefined) {
            if (!this.isExistSpan(obj, "<span value=\"" + tdata.children[m].id + "\">" + tdata.children[m].label +
                "</span><br>")) {
              obj.innerHTML += "<span value=\"" + tdata.children[m].id + "\">" + tdata.children[m].label +
                "</span><br>"
            }
          } else {
            this.orgNextUser(obj, tdata.children[m]);
          }
        }
        // }
      },
      CancelSelectNextUsers(obj, pdata) {
        if (pdata.children == undefined) {
          obj.innerHTML = obj.innerHTML.replace("<span value=\"" + pdata.id + "\">" + pdata.label + "</span><br>", "");
        } else {
          for (let m = 0; m < pdata.children.length; m++) {
            if (pdata.children[m].children == undefined)
              obj.innerHTML = obj.innerHTML.replace("<span value=\"" + pdata.children[m].id + "\">" + pdata.children[m]
                .label + "</span><br>", "");
            else
              this.CancelSelectNextUsers(obj, pdata.children[m]);
          }
        }
      },
      //判断元素是否已添加
      isExistSpan(obj, appendStr) {
        console.log("obj:" + obj.innerHTML);

        console.log("span:" + appendStr)
        console.log("indexOf:" + obj.innerHTML.indexOf(appendStr))
        if (obj.innerHTML.indexOf(appendStr) != -1) {
          return true;
        } else {
          return false;
        }
      },
      //1单选，2多选，3部门限选一个
      handleCheckChange(data, checked, indeterminate) {
        // console.log(data, checked, indeterminate);
        var obj = document.getElementById("selectedUser");
        if (checked) {
          if (data.children == undefined) {
            if (!this.isExistSpan(obj, "<span value=\"" + data.id + "\">" + data.label + "</span><br>"))
              obj.innerHTML += "<span value=\"" + data.id + "\">" + data.label + "</span><br>"
          } else {
            for (let m = 0; m < data.children.length; m++) {
              if (data.children[m].children == undefined) {
                if (!this.isExistSpan(obj, "<span value=\"" + data.children[m].id + "\">" + data.children[m].label +
                    "</span><br>"))
                  obj.innerHTML += "<span value=\"" + data.children[m].id + "\">" + data.children[m].label +
                  "</span><br>"
              } else
                this.orgNextUser(obj, data.children[m]);
            }
          }
        } else {
          console.log("delete-->obj:" + obj.innerHTML + ",indeterminate=" + indeterminate);
          console.log("delete-->span:" + "<span value=\"" + data.id + "\">" + data.label + "</span><br>");
          var obj = document.getElementById("selectedUser");
          if (data.children == undefined) {
            obj.innerHTML = obj.innerHTML.replace("<span value=\"" + data.id + "\">" + data.label + "</span><br>",
              "");
          } else {
            if (!indeterminate) {
              for (let m = 0; m < data.children.length; m++) {
                if (data.children[m].children == undefined) {
                  obj.innerHTML = obj.innerHTML.replace("<span value=\"" + data.children[m].id + "\">" + data.children[
                      m]
                    .label + "</span><br>", "");
                } else
                  this.CancelSelectNextUsers(obj, data.children[m]);
              }
            }
          }
        }
      },

      //清空已选择的数据
      clearselectedData(id) {
        document.getElementById(id).innerHTML = "";
      },

      handleNodeClick2(pdata) {
        console.log(pdata);
      },
      handleNodeClick3(pdata) {
        console.log(pdata);
      },
      // 取消按钮
      cancel() {
        this.clearselectedData("selectedUser");
        this.open = false;

      },
      saveForm() {
        alert("我是保存按钮");
      },
      //关闭弹层
      closeDialog() {
        this.clearselectedData("selectedUser");
      },
      reset() {
        this.form = {

        };
        this.resetForm("form");

      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateFileWord(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addFileWord(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      }
    }
  };
</script>

<style>
  .ProcessSubmitForm_class .el-row {
    display: flex;
    padding-top: 10px;
  }

  .ProcessSubmitForm_class .el-row .fieldset {
    /* display: flex; */
    justify-content: center;
    width: 376px;
  }

  .ProcessSubmitForm_class .el-row .fieldset .legend {
    font-size: 14px;
    color: red;
  }

  .ProcessSubmitForm_class .el-row .fieldset .div_left {
    height: 200px;
    width: 210px;
    border: 1px solid gainsboro;
    float: left;
    overflow-y: scroll;
    overflow-x: hidden;
  }

  .ProcessSubmitForm_class .el-row .fieldset .div_mid {
    height: 200px;
    width: 40px;
    float: left;
    padding-top: 100px;
    text-align: center;
  }

  .ProcessSubmitForm_class .el-row .fieldset .div_right {
    height: 200px;
    width: 100px;
    float: left;
    padding-left: 8px;
    border: 1px solid gainsboro;
    overflow-y: auto;
    overflow-x: hidden;
  }

  .ProcessSubmitForm_class .bottom-button {
    margin-top: 20px;
    width: 100%;
    text-align: right;
  }

  .ProcessSubmitForm_class .routerData-el-table {
    width: 376px;
    border: 1px solid gainsboro;
    height: 110px;
    overflow-y: scroll;
    overflow-x: hidden;
  }
</style>
